// page/person/index.js
var config = require('../../../config.js');
var loadLittle = require('loadLittle.js');
var common = require('../../utils/common.js');
var page = 0;
var size = 6;
var tab_page = {};
var seriesId = 0;
var username = '';
var app = getApp();
var idd=0;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    src: '',
    title: '',
    des: '',
    list: [],
    lastLoadTime:0,
    windowHeight:0,
    lastLoadTime: new Date().getTime(),
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: config.barTitle
    })
    var that = this;
    var id = options.id;
    idd=id;
    seriesId = id;
    //获取系列相关信息
    wx.request({
      url: config.series + '/' + id,
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        var data = res.data;
        that.setData({
          title: data.title,
          des: data.description,
          src: data.coverUrl
        });
       
      }
    });
    var data = {
      seriesId: id,
      page: page,
      size: size
    };
    loadLittle.load_list(data, page, tab_page, username, that);
    
    wx.getSystemInfo({
      success: (res) => {
        this.setData({
          windowHeight: res.windowHeight - 200
        })
      }
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    app.getUserName(function (user_name) {
      username = user_name;
      //执行，将系列与用户关联起来
      wx.request({
        url: config.series + '/' + idd + '/view',
        data: {
          username: username
        },
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {

        }
      })
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2];  //上一个页面

    prevPage.setData({
      clickSeriesId: 0
    });
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  loadMore: function () {
    var that = this;
    var currentPage = tab_page.page + 1;
    if (currentPage <= tab_page.pageSize - 1) {
      var curTime = new Date().getTime();
      //上一次加载的时间
      var lastTime = this.data.lastLoadTime;
      that.setData({
        lastLoadTime: curTime
      });
      if (curTime - lastTime < 300) {
        return;
      }
    var timeout = setTimeout(function () {
      wx.hideLoading();
      clearTimeout(timeout);
    }, 500);
    wx.showLoading({
      title: '加载中···',
    });
      var data = {
        seriesId: seriesId,
        page: currentPage,
        size: size
      };
     
      loadLittle.load_page(data, currentPage, tab_page, username, that);
    }
    var timeout = setTimeout(function () {
      wx.hideLoading();
      clearTimeout(timeout);
    }, 500);
  }, refesh: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      //title: config.shareDes,
      title:this.data.title,
      //path: '/pages/index/index',
      path: '/pages/search/seriesList/serieslist?id=' + seriesId,
      imageUrl: '../../images/sharepic_1.jpg'
    }
    // return {
    //   title: config.shareName,
    //   desc: config.shareDes,
    //   path: '/pages/search/seriesList/serieslist?id='+id
    // }
  },
  inplay: function (e) {
    var title = e.currentTarget.dataset.title;
    var id = e.currentTarget.dataset.id;
    if (id == 0 || id == null) {
      return;
    }
    var url = '../../video/video?id=' + id + '&title=' + title;
    wx.navigateTo({
      url: url
    })
  }, favor: function (e) {
    var that = this;
    var idd = e.currentTarget.dataset.id;
    var fid = e.currentTarget.dataset.fid;//是否收藏1 是，0 否
    if(idd==0||idd==null){
      return;
    }


    var value = '';
    if (fid == '1') {//表示取消收藏   
      common.add_rem_favorite(idd, username, 'remove',
        //loadRequest.render_page(idd, '0', tab_index, tab_new, tab_hot, tab_tj, that)
        common.change_tab_list(idd, '0', tab_page, that)
      );
    } else {//表示需要收藏
      common.add_rem_favorite(idd, username, 'add',
        //loadRequest.render_page(idd, '1', tab_index, tab_new, tab_hot, tab_tj, that)
        common.change_tab_list(idd, '1', tab_page, that)
      );
    }
  }
})